﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.AnalysisTools;

namespace Tayninh2011
{
    class HCalculateLandPrize
    {
        //ten huyen: Tây Ninh,Tân Biên, Tân Châu,Dương Minh Châu,Châu Thành,Hòa Thành,Bến Cầu,
        //Gò Dầu,Trảng Bàng
        private string _strTenHuyen = "";

        private string _strTenXa="";
        private int _intLoaiXa;
        private int _intKhuvucXa;
        private int _intLoaiDuong;
        //gan prefix cho vitriDat:
        //  dat nong nghiep: 1
        //  dat phi nong nghiep nong thon: 2 + _intKhuvucXa
        //  dat phi nong nghiep do thi: 3
        //  vi du: dat pnnnt o vi tri so 1 khu vuc 3: 231
        //
        //Doi voi dat do thi:
        //  dat mat tien: 301
        //  dat sau 50 m ko hem: vi tri 302
        //  dat hem:
        //      hem chinh: gan prefix la: 31
        //      hem phu: gan prefix la: 32
        //          hem sau <100m va rong >6m: 1
        //          hem sau <100m va rong 3.5-6m: 2
        //          hem sau <100m va rong <3.5m : 3
        //          hem sau 100-200m va rong >6m: 4
        //          hem sau 100-200m va rong 3.5-6m: 5
        //          hem sau 100-200m va rong <3.5m: 6
        //          hem sau >200m va rong >6m: 7
        //          hem sau >200m va rong 3.5-6m: 8
        //          hem sau >200m va rong <3.5m: 9
        private int _intVitriDat;

        private double _hesoMattienK2 = 1.2; //dat co 2 mat tien
        private double _hesoMattienK3 = 1.3; //dat co 3 mat tien
        private double _hesoMattienK4 = 1.4; //dat co 4 mat tien

        private IWorkspaceEdit _workspace;
        private IFeatureWorkspace _featureWorkspace;
        private IFeatureClass _featureClassThua;
        private string _strNameTableThua = "";
        private IFeatureClass _featureClassDuong;
        private string _strNameTableDuong = "";
        private IFeatureClass _featureClassHem;
        private string _strNameTableHem = "";
        private IFeatureClass _featureClassXa;
        private string _strNameTableXa = "";

        public HCalculateLandPrize(IWorkspaceEdit wsp, string tableThua, string tableDuong, string tableHem)
        {
            Workspace = wsp;
            FeatureWorkspace = (IFeatureWorkspace)wsp;
            StrNameTableThua = tableThua;
            StrNameTableDuong = tableDuong;
            StrNameTableHem = tableHem;
            //FeatureClassThua = GetFeatureClass(StrNameTableThua);
            //FeatureClassDuong = GetFeatureClass(StrNameTableDuong);
            //FeatureClassHem = GetFeatureClass(StrNameTableHem);
        }
        public HCalculateLandPrize(IWorkspaceEdit wsp, string tableThua, string tableDuong, string tableHem, string tableXa)
        {
            Workspace = wsp;
            FeatureWorkspace = (IFeatureWorkspace)wsp;
            StrNameTableThua = tableThua;
            StrNameTableDuong = tableDuong;
            StrNameTableHem = tableHem;
            StrNameTableXa = tableXa;
            //FeatureClassThua = GetFeatureClass(StrNameTableThua);
            //FeatureClassDuong = GetFeatureClass(StrNameTableDuong);
            //FeatureClassHem = GetFeatureClass(StrNameTableHem);
        }

        public IWorkspaceEdit Workspace
        {
            get { return _workspace; }
            set { _workspace = value; }
        }
        

        public IFeatureWorkspace FeatureWorkspace
        {
            get { return _featureWorkspace; }
            set { _featureWorkspace = value; }
        }
        

        public IFeatureClass FeatureClassThua
        {
            get { return _featureClassThua; }
            set { _featureClassThua = value; }
        }
        

        public IFeatureClass FeatureClassDuong
        {
            get { return _featureClassDuong; }
            set { _featureClassDuong = value; }
        }
        

        public IFeatureClass FeatureClassHem
        {
            get { return _featureClassHem; }
            set { _featureClassHem = value; }
        }

        public string StrNameTableThua
        {
            get { return _strNameTableThua; }
            set { _strNameTableThua = value; }
        }
        public string StrNameTableDuong
        {
            get { return _strNameTableDuong; }
            set { _strNameTableDuong = value; }
        }


        public string StrNameTableHem
        {
            get { return _strNameTableHem; }
            set { _strNameTableHem = value; }
        }
        public string StrNameTableXa
        {
            get { return _strNameTableXa; }
            set { _strNameTableXa = value; }
        }

        public IFeatureClass FeatureClassXa
        {
            get { return _featureClassXa; }
            set { _featureClassXa = value; }
        }

        private IFeatureCursor querySpatial(string table1, esriSpatialRelEnum dieukien, IGeometry featureFromTable2)
        {
            IFeatureClass featureClass = FeatureWorkspace.OpenFeatureClass(table1);
            //IFeatureClass featureClass2 = FeatureWorkspace.OpenFeatureClass(table2);
            
            IFeatureCursor featureCursor;
            ISpatialFilter spatialFilter = new SpatialFilterClass();

            spatialFilter.Geometry = featureFromTable2;
            spatialFilter.GeometryField = featureClass.ShapeFieldName;
            spatialFilter.SpatialRel = dieukien;
            featureCursor = featureClass.Search(spatialFilter, false);

            return featureCursor;
        }

        public IFeatureClass GetFeatureClass(string table)
        {
            IFeatureClass featureClass = FeatureWorkspace.OpenFeatureClass(table);
            return featureClass;
        }

        private ICursor queryAttribute(string table, string fields, string dieukien)
        {
            IQueryFilter queryFilter = new QueryFilterClass();
            IQueryDef queryDef = FeatureWorkspace.CreateQueryDef();

            //cac truong tra ve
            //queryFilter.SubFields="SO_THUA, TENCHU";
            queryDef.Tables = table;
            queryDef.SubFields = "SO_THUA,TENCHU";
            queryDef.WhereClause = dieukien;
            ICursor cursor = queryDef.Evaluate();

            //queryFilter.WhereClause = dieukien;

            //IQueryFilterDefinition queryFilterDef = (IQueryFilterDefinition)queryFilter;
            //queryFilterDef.PostfixClause = "ORDER BY SO_TO";
            
            //ICursor cursor = table.Search(queryFilter, false);
            return cursor;
        }
        public IFeatureClass Buffer(IFeatureClass input, uint distance, string side_type, string end_type, string dissolve_type, IField dissolve_field)
        {
            try
            {
                
                //tao moi output trong csdl
                //IFeatureClassDescription featureClassDesc = new FeatureClassDescriptionClass();
                //IObjectClassDescription objectClassDesc = new ObjectClassDescriptionClass();
                //IFieldChecker fieldChecker = new FieldCheckerClass();
                //IEnumFieldError enumFieldError = null;
                //fieldChecker.ValidateWorkspace = Workspace;

                Geoprocessor gp = new Geoprocessor();
                //gp.SetEnvironmentValue("workspace", Workspace.WorkspaceFactory.);
                ESRI.ArcGIS.AnalysisTools.Buffer bufferTool = new ESRI.ArcGIS.AnalysisTools.Buffer();
                bufferTool.in_features = input;
                bufferTool.out_feature_class ="duong_buffer_50";
                bufferTool.buffer_distance_or_field = distance;
                bufferTool.line_side = side_type;
                bufferTool.line_end_type = end_type;
                bufferTool.dissolve_option = "ALL";
                bufferTool.dissolve_field = dissolve_field;
                gp.Execute(bufferTool, null);
            }
            catch(Exception err)
            {
                MessageBox.Show(err.ToString());
            }
            
            try
            {
                IFeatureClass output;
                output = FeatureWorkspace.OpenFeatureClass("duong_buffer_50");
                return output;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
            }
            
            return null;
        }
        public IFeatureClass Buffer(IFeatureClass input, uint distance, string side_type, string end_type)
        {
            try
            {
                Geoprocessor gp = new Geoprocessor();
                IDataset dataSet = (IDataset)Workspace;
                string sde="C:/P2.mdb/P2" ;//"Database Connections/sde.sde";
                //gp.SetEnvironmentValue("workspace", sde);
                
                ESRI.ArcGIS.AnalysisTools.Buffer bufferTool = new ESRI.ArcGIS.AnalysisTools.Buffer();
                bufferTool.in_features = @"C:\P2.mdb\P2\duong";
                bufferTool.out_feature_class = @"C:\P2.mdb\P2\duongbuff";
                //bufferTool.line_side = side_type;
                //bufferTool.line_end_type = end_type;
                //bufferTool.dissolve_option = "NONE";
                bufferTool.buffer_distance_or_field = 50;
                gp.Execute(bufferTool, null);
            }
            catch(Exception err)
            {
                MessageBox.Show(err.ToString());
            }
            try
            {
                IFeatureClass output;
                //output = FeatureWorkspace.OpenFeatureClass("duongbuffer50");
                return null;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
            }
            return null;
        }
    }
}
